home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Data / Dumper.pm
Encoding:
Perl POD Document  |  1999-12-28  |  24.4 KB  |  828 lines

  1.  
  2. package Data::Dumper;
  3.  
  4. $VERSION = $VERSION = '2.07';
  5.  
  6.  
  7. require 5.002;
  8. require Exporter;
  9. require DynaLoader;
  10.  
  11. use Carp;
  12.  
  13. @ISA = qw(Exporter DynaLoader);
  14. @EXPORT = qw(Dumper);
  15. @EXPORT_OK = qw(DumperX);
  16.  
  17. bootstrap Data::Dumper;
  18.  
  19. $Indent = 2 unless defined $Indent;
  20. $Purity = 0 unless defined $Purity;
  21. $Pad = "" unless defined $Pad;
  22. $Varname = "VAR" unless defined $Varname;
  23. $Useqq = 0 unless defined $Useqq;
  24. $Terse = 0 unless defined $Terse;
  25. $Freezer = "" unless defined $Freezer;
  26. $Toaster = "" unless defined $Toaster;
  27. $Deepcopy = 0 unless defined $Deepcopy;
  28.  
  29. sub new {
  30.   my($c, $v, $n) = @_;
  31.  
  32.   croak "Usage:  PACKAGE->new(ARRAYREF, [ARRAYREF])" 
  33.     unless (defined($v) && (ref($v) eq 'ARRAY'));
  34.   $n = [] unless (defined($n) && (ref($v) eq 'ARRAY'));
  35.  
  36.   my($s) = { 
  37.              level      => 0,           # current recursive depth
  38.          indent     => $Indent,     # various styles of indenting
  39.          pad    => $Pad,        # all lines prefixed by this string
  40.          xpad       => "",          # padding-per-level
  41.          apad       => "",          # added padding for hash keys n such
  42.          sep        => "",          # list separator
  43.          seen       => {},          # local (nested) refs (id => [name, val])
  44.          todump     => $v,          # values to dump []
  45.          names      => $n,          # optional names for values []
  46.          varname    => $Varname,    # prefix to use for tagging nameless ones
  47.              purity     => $Purity,     # degree to which output is evalable
  48.              useqq     => $Useqq,      # use "" for strings (backslashitis ensues)
  49.              terse     => $Terse,      # avoid name output (where feasible)
  50.              freezer    => $Freezer,    # name of Freezer method for objects
  51.              toaster    => $Toaster,    # name of method to revive objects
  52.              deepcopy    => $Deepcopy,   # dont cross-ref, except to stop recursion
  53.        };
  54.  
  55.   if ($Indent > 0) {
  56.     $s->{xpad} = "  ";
  57.     $s->{sep} = "\n";
  58.   }
  59.   return bless($s, $c);
  60. }
  61.  
  62. sub Seen {
  63.   my($s, $g) = @_;
  64.   if (defined($g) && (ref($g) eq 'HASH'))  {
  65.     my($k, $v, $id);
  66.     while (($k, $v) = each %$g) {
  67.       if (defined $v and ref $v) {
  68.     ($id) = ("$v" =~ /\((.*)\)$/);
  69.     if ($k =~ /^[*](.*)$/) {
  70.       $k = (ref $v eq 'ARRAY') ? ( "\\\@" . $1 ) :
  71.         (ref $v eq 'HASH') ? ( "\\\%" . $1 ) :
  72.           ("\$" . $1 );
  73.     }
  74.     elsif ($k !~ /^\$/) {
  75.       $k = "\$" . $k;
  76.     }
  77.     $s->{seen}{$id} = [$k, $v];
  78.       }
  79.       else {
  80.     carp "Only refs supported, ignoring non-ref item \$$k";
  81.       }
  82.     }
  83.   }
  84.   else {
  85.     return map { @$_ } values %{$s->{seen}};
  86.   }
  87. }
  88.  
  89. sub Values {
  90.   my($s, $v) = @_;
  91.   if (defined($v) && (ref($v) eq 'ARRAY'))  {
  92.     $s->{todump} = [@$v];        # make a copy
  93.   }
  94.   else {
  95.     return @{$s->{todump}};
  96.   }
  97. }
  98.  
  99. sub Names {
  100.   my($s, $n) = @_;
  101.   if (defined($n) && (ref($n) eq 'ARRAY'))  {
  102.     $s->{names} = [@$n];         # make a copy
  103.   }
  104.   else {
  105.     return @{$s->{names}};
  106.   }
  107. }
  108.  
  109. sub DESTROY {}
  110.  
  111. sub Dump {
  112.   my($s) = shift;
  113.   my(@out, $val, $name);
  114.   my($i) = 0;
  115.   local(@post);
  116.  
  117.   $s = $s->new(@_) unless ref $s;
  118.  
  119.   for $val (@{$s->{todump}}) {
  120.     my $out = "";
  121.     @post = ();
  122.     $name = $s->{names}[$i++];
  123.     if (defined $name) {
  124.       if ($name =~ /^[*](.*)$/) {
  125.     if (defined $val) {
  126.       $name = (ref $val eq 'ARRAY') ? ( "\@" . $1 ) :
  127.         (ref $val eq 'HASH') ? ( "\%" . $1 ) :
  128.           ("\$" . $1 );
  129.     }
  130.     else {
  131.       $name = "\$" . $1;
  132.     }
  133.       }
  134.       elsif ($name !~ /^\$/) {
  135.     $name = "\$" . $name;
  136.       }
  137.     }
  138.     else {
  139.       $name = "\$" . $s->{varname} . $i;
  140.     }
  141.  
  142.     my $valstr;
  143.     {
  144.       local($s->{apad}) = $s->{apad};
  145.       $s->{apad} .= ' ' x (length($name) + 3) if $s->{indent} >= 2;
  146.       $valstr = $s->_dump($val, $name);
  147.     }
  148.  
  149.     $valstr = "$name = " . $valstr . ';' if @post or !$s->{terse};
  150.     $out .= $s->{pad} . $valstr . $s->{sep};
  151.     $out .= $s->{pad} . join(';' . $s->{sep} . $s->{pad}, @post) 
  152.       . ';' . $s->{sep} if @post;
  153.  
  154.     push @out, $out;
  155.   }
  156.   return wantarray ? @out : join('', @out);
  157. }
  158.  
  159. sub _dump {
  160.   my($s, $val, $name) = @_;
  161.   my($sname);
  162.   my($out, $realpack, $realtype, $type, $ipad, $id, $blesspad);
  163.  
  164.   return "undef" unless defined $val;
  165.  
  166.   $type = ref $val;
  167.   $out = "";
  168.  
  169.   if ($type) {
  170.  
  171.     if ($type =~ /[a-z_:]/) {
  172.       my $freezer = $s->{freezer};
  173.       if ($freezer) {
  174.     eval { $val->$freezer() };
  175.     carp "WARNING(Freezer method call failed): $@" if $@;
  176.       }
  177.     }
  178.  
  179.     if (exists $ {$type . "::OVERLOAD"}{'""'}) {
  180.       bless $val, "Dumper::FAKE";
  181.       ($realpack, $realtype, $id) = ("$val" =~ /^(?:(.*)\=)?([^=]*)\(([^\(]*)\)$/);
  182.       bless $val, $type;
  183.       $realpack = $type;
  184.     }
  185.     else {
  186.       ($realpack, $realtype, $id) = ("$val" =~ /^(?:(.*)\=)?([^=]*)\(([^\(]*)\)$/);
  187.     }
  188.     
  189.     if (exists $s->{seen}{$id}) {
  190.       if ($s->{purity} and $s->{level} > 0) {
  191.     $out = ($realtype eq 'HASH') ? '{}' :
  192.       ($realtype eq 'ARRAY') ? '[]' : "''";
  193.       push @post, $name . " = " . $s->{seen}{$id}[0];
  194.       }
  195.       else {
  196.     $out = $s->{seen}{$id}[0];
  197.     if ($name =~ /^([\@\%])/) {
  198.       my $start = $1;
  199.       if ($out =~ /^\\$start/) {
  200.         $out = substr($out, 1);
  201.       }
  202.       else {
  203.         $out = $start . '{' . $out . '}';
  204.       }
  205.     }
  206.       }
  207.       return $out;
  208.     }
  209.     else {
  210.       $s->{seen}{$id} = [ ($name =~ /^[@%].*$/) ? ('\\' . $name ) : $name, $val ];
  211.     }
  212.  
  213.     $s->{level}++;
  214.     $ipad = $s->{xpad} x $s->{level};
  215.  
  216.     if ($realpack) {          # we have a blessed ref
  217.       $out = 'bless( ';
  218.       $blesspad = $s->{apad};
  219.       $s->{apad} .= '       ' if ($s->{indent} >= 2);
  220.     }
  221.     
  222.     if ($realtype eq 'SCALAR') {
  223.       if ($realpack) {
  224.     $out .= '\\' . '($_ = ' . $s->_dump($$val, "") . ')';
  225.       }
  226.       else {
  227.     $out .= '\\' . $s->_dump($$val, "");
  228.       }
  229.     }
  230.     elsif ($realtype eq 'GLOB') {
  231.     $out .= '\\' . $s->_dump($$val, "");
  232.     }
  233.     elsif ($realtype eq 'ARRAY') {
  234.       my($v, $pad, $mname);
  235.       my($i) = 0;
  236.       $out .= ($name =~ /^\@/) ? '(' : '[';
  237.       $pad = $s->{sep} . $s->{pad} . $s->{apad};
  238.       ($name =~ /^\@(.*)$/) ? ($mname = "\$" . $1) : 
  239.     ($name =~ /[]}]$/) ? ($mname = $name) : ($mname = $name . '->');
  240.       $mname .= '->' if $mname =~ /^\*.+\{[A-Z]+\}$/;
  241.       for $v (@$val) {
  242.     $sname = $mname . '[' . $i . ']';
  243.     $out .= $pad . $ipad . '#' . $i if $s->{indent} >= 3;
  244.     $out .= $pad . $ipad . $s->_dump($v, $sname);
  245.     $out .= "," if $i++ < $#$val;
  246.       }
  247.       $out .= $pad . ($s->{xpad} x ($s->{level} - 1)) if $i;
  248.       $out .= ($name =~ /^\@/) ? ')' : ']';
  249.     }
  250.     elsif ($realtype eq 'HASH') {
  251.       my($k, $v, $pad, $lpad, $mname);
  252.       $out .= ($name =~ /^\%/) ? '(' : '{';
  253.       $pad = $s->{sep} . $s->{pad} . $s->{apad};
  254.       $lpad = $s->{apad};
  255.       ($name =~ /^\%(.*)$/) ? ($mname = "\$" . $1) : 
  256.     ($name =~ /[]}]$/) ? ($mname = $name) : ($mname = $name . '->');
  257.       $mname .= '->' if $mname =~ /^\*.+\{[A-Z]+\}$/;
  258.       while (($k, $v) = each %$val) {
  259.     my $nk = $s->_dump($k, "");
  260.     $nk = $1 if !$s->{purity} and $nk =~ /^[\"\']([A-Za-z_][\w:]*)[\"\']$/;
  261.     $sname = $mname . '{' . $nk . '}';
  262.     $out .= $pad . $ipad . $nk . " => ";
  263.  
  264.     $s->{apad} .= (" " x (length($nk) + 4)) if $s->{indent} >= 2;
  265.     $out .= $s->_dump($val->{$k}, $sname) . ",";
  266.     $s->{apad} = $lpad if $s->{indent} >= 2;
  267.       }
  268.       if (substr($out, -1) eq ',') {
  269.     chop $out;
  270.     $out .= $pad . ($s->{xpad} x ($s->{level} - 1));
  271.       }
  272.       $out .= ($name =~ /^\%/) ? ')' : '}';
  273.     }
  274.     elsif ($realtype eq 'CODE') {
  275.       $out .= '"DUMMY"';
  276.       $out = 'sub { ' . $out . ' }';
  277.       carp "Encountered CODE ref, using dummy placeholder" if $s->{purity};
  278.     }
  279.     else {
  280.       croak "Can\'t handle $realtype type.";
  281.     }
  282.     
  283.     if ($realpack) { # we have a blessed ref
  284.       $out .= ', \'' . $realpack . '\'' . ' )';
  285.       $out .= '->' . $s->{toaster} . '()'  if $s->{toaster} ne '';
  286.       $s->{apad} = $blesspad;
  287.     }
  288.     $s->{level}--;
  289.  
  290.   }
  291.   else {                                 # simple scalar
  292.  
  293.     my $ref = \$_[1];
  294.     if ($name ne '') {
  295.       ($id) = ("$ref" =~ /\(([^\(]*)\)$/);
  296.       if (exists $s->{seen}{$id}) {
  297.     $out = $s->{seen}{$id}[0];
  298.     return $out;
  299.       }
  300.       else {
  301.     $s->{seen}{$id} = ["\\$name", $val];
  302.       }
  303.     }
  304.     if (ref($ref) eq 'GLOB' or "$ref" =~ /=GLOB\([^()]+\)$/) {  # glob
  305.       my $name = substr($val, 1);
  306.       if ($name =~ /^[A-Za-z_][\w:]*$/) {
  307.     $name =~ s/^main::/::/;
  308.     $sname = $name;
  309.       }
  310.       else {
  311.     $sname = $s->_dump($name, "");
  312.     $sname = '{' . $sname . '}';
  313.       }
  314.       if ($s->{purity}) {
  315.     my $k;
  316.     local ($s->{level}) = 0;
  317.     for $k (qw(SCALAR ARRAY HASH)) {
  318.       my $postlen = scalar @post;
  319.       $post[$postlen] = "\*$sname = ";
  320.       local ($s->{apad}) = " " x length($post[$postlen]) if $s->{indent} >= 2;
  321.       $post[$postlen] .= $s->_dump(*{$name}{$k}, "\*$sname\{$k\}");
  322.     }
  323.       }
  324.       $out .= '*' . $sname;
  325.     }
  326.     elsif ($val =~ /^-?[1-9]\d{0,8}$/) { # safe decimal number
  327.       $out .= $val;
  328.     }
  329.     else {                 # string
  330.       if ($s->{useqq}) {
  331.     $out .= qquote($val);
  332.       }
  333.       else {
  334.     $val =~ s/([\\\'])/\\$1/g;
  335.     $out .= '\'' . $val .  '\'';
  336.       }
  337.     }
  338.   }
  339.  
  340.   delete($s->{seen}{$id}) if $id and $s->{deepcopy};
  341.   return $out;
  342. }
  343.   
  344. sub Dumper {
  345.   return Data::Dumper->Dump([@_]);
  346. }
  347.  
  348. sub DumperX {
  349.   return Data::Dumper->Dumpxs([@_], []);
  350. }
  351.  
  352. sub Dumpf { return Data::Dumper->Dump(@_) }
  353.  
  354. sub Dumpp { print Data::Dumper->Dump(@_) }
  355.  
  356. sub Reset {
  357.   my($s) = shift;
  358.   $s->{seen} = {};
  359. }
  360.  
  361. sub Indent {
  362.   my($s, $v) = @_;
  363.   if (defined($v)) {
  364.     if ($v == 0) {
  365.       $s->{xpad} = "";
  366.       $s->{sep} = "";
  367.     }
  368.     else {
  369.       $s->{xpad} = "  ";
  370.       $s->{sep} = "\n";
  371.     }
  372.     $s->{indent} = $v;
  373.   }
  374.   return $s->{indent};
  375. }
  376.  
  377. sub Pad {
  378.   my($s, $v) = @_;
  379.   defined($v) ? ($s->{pad} = $v) : $s->{pad};
  380. }
  381.  
  382. sub Varname {
  383.   my($s, $v) = @_;
  384.   defined($v) ? ($s->{varname} = $v) : $s->{varname};
  385. }
  386.  
  387. sub Purity {
  388.   my($s, $v) = @_;
  389.   defined($v) ? ($s->{purity} = $v) : $s->{purity};
  390. }
  391.  
  392. sub Useqq {
  393.   my($s, $v) = @_;
  394.   defined($v) ? ($s->{useqq} = $v) : $s->{useqq};
  395. }
  396.  
  397. sub Terse {
  398.   my($s, $v) = @_;
  399.   defined($v) ? ($s->{terse} = $v) : $s->{terse};
  400. }
  401.  
  402. sub Freezer {
  403.   my($s, $v) = @_;
  404.   defined($v) ? ($s->{freezer} = $v) : $s->{freezer};
  405. }
  406.  
  407. sub Toaster {
  408.   my($s, $v) = @_;
  409.   defined($v) ? ($s->{toaster} = $v) : $s->{toaster};
  410. }
  411.  
  412. sub Deepcopy {
  413.   my($s, $v) = @_;
  414.   defined($v) ? ($s->{deepcopy} = $v) : $s->{deepcopy};
  415. }
  416.  
  417. sub qquote {
  418.   local($_) = shift;
  419.   s/([\\\"\@\$\%])/\\$1/g;    
  420.   s/\a/\\a/g;
  421.   s/[\b]/\\b/g;
  422.   s/\t/\\t/g;
  423.   s/\n/\\n/g;
  424.   s/\f/\\f/g;
  425.   s/\r/\\r/g;
  426.   s/\e/\\e/g;
  427.  
  428.   s/([\000-\006\013\016-\032\034-\037\177\200-\377])/'\\'.sprintf('%03o',ord($1))/eg;
  429.   return "\"$_\"";
  430. }
  431.  
  432. 1;
  433. __END__
  434.  
  435. =head1 NAME
  436.  
  437. Data::Dumper - stringified perl data structures, suitable for both printing and C<eval>
  438.  
  439.  
  440. =head1 SYNOPSIS
  441.  
  442.     use Data::Dumper;
  443.  
  444.     print Dumper($foo, $bar);
  445.  
  446.     print Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]);
  447.  
  448.     {
  449.       local $Data::Dump::Purity = 1;
  450.       eval Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]);
  451.     }
  452.  
  453.     $d = Data::Dumper->new([$foo, $bar], [qw(foo *ary)]);
  454.        ...
  455.     print $d->Dump;
  456.        ...
  457.     $d->Purity(1);
  458.     $d->Terse(1);
  459.     $d->Deepcopy(1);
  460.     eval $d->Dump;
  461.  
  462.  
  463. =head1 DESCRIPTION
  464.  
  465. Given a list of scalars or reference variables, writes out their contents in
  466. perl syntax. The references can also be objects.  The contents of each
  467. variable is output in a single Perl statement.
  468.  
  469. The return value can be C<eval>ed to get back the original reference
  470. structure. Bear in mind that a reference so created will not preserve
  471. pointer equalities with the original reference.
  472.  
  473. Handles self-referential structures correctly.  Any references that are the
  474. same as one of those passed in will be marked C<$VAR>I<n> (where I<n> is a
  475. numeric suffix), and other duplicate references to substructures within 
  476. C<$VAR>I<n> will be appropriately labeled using arrow notation.  You can
  477. specify names for individual values to be dumped if you use the C<Dump()>
  478. method, or you can change the default C<$VAR> prefix to something else.  See
  479. L<$Data::Dumper::Varname> and L<$Data::Dumper::Terse> below.
  480.  
  481. The default output of self-referential structures can be C<eval>ed, but the
  482. nested references to C<$VAR>I<n> will be undefined, since a recursive
  483. structure cannot be constructed using one Perl statement.  You can set the
  484. C<Purity> flag to 1 to get additional statements that will correctly fill in
  485. these references.
  486.  
  487. In the extended usage form, the references to be dumped can be given
  488. user-specified names.  If a name begins with a C<*>, the output will 
  489. describe the dereferenced type of the supplied reference for hashes and
  490. arrays.  Output of names will be avoided where possible if the C<Terse>
  491. flag is set.
  492.  
  493. Several styles of output are possible, all controlled by setting
  494. the C<Indent> flag.  See L<Configuration Variables or Methods> below 
  495. for details.
  496.  
  497.  
  498. =head2 Methods
  499.  
  500. =over 4
  501.  
  502. =item I<PACKAGE>->new(I<ARRAYREF [>, I<ARRAYREF]>)
  503.  
  504. Returns a newly created C<Data::Dumper> object.  The first argument is an
  505. anonymous array of values to be dumped.  The optional second argument is an
  506. anonymous array of names for the values.  The names need not have a leading
  507. C<$> sign, and must be comprised of alphanumeric characters.  You can begin
  508. a name with a C<*> to specify that the dereferenced type must be dumped
  509. instead of the reference itself, for ARRAY and HASH references.
  510.  
  511. The prefix specified by C<$Data::Dumper::Varname> will be used with a
  512. numeric suffix if the name for a value is undefined.
  513.  
  514. Data::Dumper will catalog all references encountered while dumping the
  515. values. Cross-references (in the form of names of substructures in perl
  516. syntax) will be inserted at all possible points, preserving any structural
  517. interdependencies in the original set of values.  Structure traversal is
  518. depth-first,  and proceeds in order from the first supplied value to
  519. the last.
  520.  
  521. =item I<$OBJ>->Dump  I<or>  I<PACKAGE>->Dump(I<ARRAYREF [>, I<ARRAYREF]>)
  522.  
  523. Returns the stringified form of the values stored in the object (preserving
  524. the order in which they were supplied to C<new>), subject to the
  525. configuration options below.  In an array context, it returns a list
  526. of strings corresponding to the supplied values.
  527.  
  528. The second form, for convenience, simply calls the C<new> method on its
  529. arguments before dumping the object immediately.
  530.  
  531. =item I<$OBJ>->Dumpxs  I<or>  I<PACKAGE>->Dumpxs(I<ARRAYREF [>, I<ARRAYREF]>)
  532.  
  533. This method is available if you were able to compile and install the XSUB
  534. extension to C<Data::Dumper>. It is exactly identical to the C<Dump> method 
  535. above, only about 4 to 5 times faster, since it is written entirely in C.
  536.  
  537. =item I<$OBJ>->Seen(I<[HASHREF]>)
  538.  
  539. Queries or adds to the internal table of already encountered references.
  540. You must use C<Reset> to explicitly clear the table if needed.  Such
  541. references are not dumped; instead, their names are inserted wherever they
  542. are encountered subsequently.
  543.  
  544. Expects a anonymous hash of name => value pairs.  Same rules apply for names
  545. as in C<new>.  If no argument is supplied, will return the "seen" list of
  546. name => value pairs, in an array context.
  547.  
  548. =item I<$OBJ>->Values(I<[ARRAYREF]>)
  549.  
  550. Queries or replaces the internal array of values that will be dumped.
  551.  
  552. =item I<$OBJ>->Names(I<[ARRAYREF]>)
  553.  
  554. Queries or replaces the internal array of user supplied names for the values
  555. that will be dumped.
  556.  
  557. =item I<$OBJ>->Reset
  558.  
  559. Clears the internal table of "seen" references.
  560.  
  561. =back
  562.  
  563. =head2 Functions
  564.  
  565. =over 4
  566.  
  567. =item Dumper(I<LIST>)
  568.  
  569. Returns the stringified form of the values in the list, subject to the
  570. configuration options below.  The values will be named C<$VAR>I<n> in the
  571. output, where I<n> is a numeric suffix.  Will return a list of strings
  572. in an array context.
  573.  
  574. =item DumperX(I<LIST>)
  575.  
  576. Identical to the C<Dumper()> function above, but this calls the XSUB 
  577. implementation.  Only available if you were able to compile and install
  578. the XSUB extensions in C<Data::Dumper>.
  579.  
  580. =back
  581.  
  582. =head2 Configuration Variables or Methods
  583.  
  584. Several configuration variables can be used to control the kind of output
  585. generated when using the procedural interface.  These variables are usually
  586. C<local>ized in a block so that other parts of the code are not affected by
  587. the change.  
  588.  
  589. These variables determine the default state of the object created by calling
  590. the C<new> method, but cannot be used to alter the state of the object
  591. thereafter.  The equivalent method names should be used instead to query
  592. or set the internal state of the object.
  593.  
  594. =over 4
  595.  
  596. =item $Data::Dumper::Indent  I<or>  I<$OBJ>->Indent(I<[NEWVAL]>)
  597.  
  598. Controls the style of indentation.  It can be set to 0, 1, 2 or 3.  Style 0
  599. spews output without any newlines, indentation, or spaces between list
  600. items.  It is the most compact format possible that can still be called
  601. valid perl.  Style 1 outputs a readable form with newlines but no fancy
  602. indentation (each level in the structure is simply indented by a fixed
  603. amount of whitespace).  Style 2 (the default) outputs a very readable form
  604. which takes into account the length of hash keys (so the hash value lines
  605. up).  Style 3 is like style 2, but also annotates the elements of arrays
  606. with their index (but the comment is on its own line, so array output
  607. consumes twice the number of lines).  Style 2 is the default.
  608.  
  609. =item $Data::Dumper::Purity  I<or>  I<$OBJ>->Purity(I<[NEWVAL]>)
  610.  
  611. Controls the degree to which the output can be C<eval>ed to recreate the
  612. supplied reference structures.  Setting it to 1 will output additional perl
  613. statements that will correctly recreate nested references.  The default is
  614. 0.
  615.  
  616. =item $Data::Dumper::Pad  I<or>  I<$OBJ>->Pad(I<[NEWVAL]>)
  617.  
  618. Specifies the string that will be prefixed to every line of the output.
  619. Empty string by default.
  620.  
  621. =item $Data::Dumper::Varname  I<or>  I<$OBJ>->Varname(I<[NEWVAL]>)
  622.  
  623. Contains the prefix to use for tagging variable names in the output. The
  624. default is "VAR".
  625.  
  626. =item $Data::Dumper::Useqq  I<or>  I<$OBJ>->Useqq(I<[NEWVAL]>)
  627.  
  628. When set, enables the use of double quotes for representing string values.
  629. Whitespace other than space will be represented as C<[\n\t\r]>, "unsafe"
  630. characters will be backslashed, and unprintable characters will be output as
  631. quoted octal integers.  Since setting this variable imposes a performance
  632. penalty, the default is 0.  The C<Dumpxs()> method does not honor this
  633. flag yet.
  634.  
  635. =item $Data::Dumper::Terse  I<or>  I<$OBJ>->Terse(I<[NEWVAL]>)
  636.  
  637. When set, Data::Dumper will emit single, non-self-referential values as
  638. atoms/terms rather than statements.  This means that the C<$VAR>I<n> names
  639. will be avoided where possible, but be advised that such output may not
  640. always be parseable by C<eval>.
  641.  
  642. =item $Data::Dumper::Freezer  I<or>  $I<OBJ>->Freezer(I<[NEWVAL]>)
  643.  
  644. Can be set to a method name, or to an empty string to disable the feature.
  645. Data::Dumper will invoke that method via the object before attempting to
  646. stringify it.  This method can alter the contents of the object (if, for
  647. instance, it contains data allocated from C), and even rebless it in a
  648. different package.  The client is responsible for making sure the specified
  649. method can be called via the object, and that the object ends up containing
  650. only perl data types after the method has been called.  Defaults to an empty
  651. string.
  652.  
  653. =item $Data::Dumper::Toaster  I<or>  $I<OBJ>->Toaster(I<[NEWVAL]>)
  654.  
  655. Can be set to a method name, or to an empty string to disable the feature.
  656. Data::Dumper will emit a method call for any objects that are to be dumped
  657. using the syntax C<bless(DATA, CLASS)->METHOD()>.  Note that this means that
  658. the method specified will have to perform any modifications required on the
  659. object (like creating new state within it, and/or reblessing it in a
  660. different package) and then return it.  The client is responsible for making
  661. sure the method can be called via the object, and that it returns a valid
  662. object.  Defaults to an empty string.
  663.  
  664. =item $Data::Dumper::Deepcopy  I<or>  $I<OBJ>->Deepcopy(I<[NEWVAL]>)
  665.  
  666. Can be set to a boolean value to enable deep copies of structures.
  667. Cross-referencing will then only be done when absolutely essential
  668. (i.e., to break reference cycles).  Default is 0.
  669.  
  670. =back
  671.  
  672. =head2 Exports
  673.  
  674. =item Dumper
  675.  
  676.  
  677. =head1 EXAMPLE
  678.  
  679. Run these code snippets to get a quick feel for the behavior of this
  680. module.  When you are through with these examples, you may want to
  681. add or change the various configuration variables described above,
  682. to see their behavior.  (See the testsuite in the Data::Dumper
  683. distribution for more examples.)
  684.  
  685.  
  686.     use Data::Dumper;
  687.  
  688.     package Foo;
  689.     sub new {bless {'a' => 1, 'b' => sub { return "foo" }}, $_[0]};
  690.  
  691.     package Fuz;                       # a weird REF-REF-SCALAR object
  692.     sub new {bless \($_ = \ 'fu\'z'), $_[0]};
  693.  
  694.     package main;
  695.     $foo = Foo->new;
  696.     $fuz = Fuz->new;
  697.     $boo = [ 1, [], "abcd", \*foo,
  698.              {1 => 'a', 023 => 'b', 0x45 => 'c'}, 
  699.              \\"p\q\'r", $foo, $fuz];
  700.     
  701.  
  702.     $bar = eval(Dumper($boo));
  703.     print($@) if $@;
  704.     print Dumper($boo), Dumper($bar);  # pretty print (no array indices)
  705.  
  706.     $Data::Dumper::Terse = 1;          # don't output names where feasible
  707.     $Data::Dumper::Indent = 0;         # turn off all pretty print
  708.     print Dumper($boo), "\n";
  709.  
  710.     $Data::Dumper::Indent = 1;         # mild pretty print
  711.     print Dumper($boo);
  712.  
  713.     $Data::Dumper::Indent = 3;         # pretty print with array indices
  714.     print Dumper($boo);
  715.  
  716.     $Data::Dumper::Useqq = 1;          # print strings in double quotes
  717.     print Dumper($boo);
  718.     
  719.     
  720.     
  721.     
  722.     @c = ('c');
  723.     $c = \@c;
  724.     $b = {};
  725.     $a = [1, $b, $c];
  726.     $b->{a} = $a;
  727.     $b->{b} = $a->[1];
  728.     $b->{c} = $a->[2];
  729.     print Data::Dumper->Dump([$a,$b,$c], [qw(a b c)]);
  730.     
  731.     
  732.     $Data::Dumper::Purity = 1;         # fill in the holes for eval
  733.     print Data::Dumper->Dump([$a, $b], [qw(*a b)]); # print as @a
  734.     print Data::Dumper->Dump([$b, $a], [qw(*b a)]); # print as %b
  735.     
  736.     
  737.     $Data::Dumper::Deepcopy = 1;       # avoid cross-refs
  738.     print Data::Dumper->Dump([$b, $a], [qw(*b a)]);
  739.     
  740.     
  741.     $Data::Dumper::Purity = 0;         # avoid cross-refs
  742.     print Data::Dumper->Dump([$b, $a], [qw(*b a)]);
  743.     
  744.     
  745.     
  746.     
  747.     $d = Data::Dumper->new([$a,$b], [qw(a b)]);
  748.     $d->Seen({'*c' => $c});            # stash a ref without printing it
  749.     $d->Indent(3);
  750.     print $d->Dump;
  751.     $d->Reset;                         # empty the seen cache
  752.     $d->Purity(0);
  753.     print join "----\n", $d->Dump;
  754.     
  755.     
  756.     
  757.     
  758.     package Foo;
  759.     sub new { bless { state => 'awake' }, shift }
  760.     sub Freeze {
  761.         my $s = shift;
  762.     print STDERR "preparing to sleep\n";
  763.     $s->{state} = 'asleep';
  764.     return bless $s, 'Foo::ZZZ';
  765.     }
  766.     
  767.     package Foo::ZZZ;
  768.     sub Thaw {
  769.         my $s = shift;
  770.     print STDERR "waking up\n";
  771.     $s->{state} = 'awake';
  772.     return bless $s, 'Foo';
  773.     }
  774.     
  775.     package Foo;
  776.     use Data::Dumper;
  777.     my $a = Foo->new;
  778.     my $b = Data::Dumper->new([$a], ['c']);
  779.     $b->Freezer('Freeze');
  780.     $b->Toaster('Thaw');
  781.     my $c = $b->Dump;
  782.     print $c;
  783.     my $d = eval $c;
  784.     print Data::Dumper->Dump([$d], ['d']);
  785.  
  786.  
  787. =head1 BUGS
  788.  
  789. Due to limitations of Perl subroutine call semantics, you cannot pass an
  790. array or hash.  Prepend it with a C<\> to pass its reference instead.  This
  791. will be remedied in time, with the arrival of prototypes in later versions
  792. of Perl.  For now, you need to use the extended usage form, and prepend the
  793. name with a C<*> to output it as a hash or array.
  794.  
  795. C<Data::Dumper> cheats with CODE references.  If a code reference is
  796. encountered in the structure being processed, an anonymous subroutine that
  797. contains the string '"DUMMY"' will be inserted in its place, and a warning
  798. will be printed if C<Purity> is set.  You can C<eval> the result, but bear
  799. in mind that the anonymous sub that gets created is just a placeholder.
  800. Someday, perl will have a switch to cache-on-demand the string
  801. representation of a compiled piece of code, I hope.
  802.  
  803. The C<Useqq> flag is not honored by C<Dumpxs()> (it always outputs
  804. strings in single quotes).
  805.  
  806. SCALAR objects have the weirdest looking C<bless> workaround.
  807.  
  808.  
  809. =head1 AUTHOR
  810.  
  811. Gurusamy Sarathy        gsar@umich.edu
  812.  
  813. Copyright (c) 1996 Gurusamy Sarathy. All rights reserved.
  814. This program is free software; you can redistribute it and/or
  815. modify it under the same terms as Perl itself.
  816.  
  817.  
  818. =head1 VERSION
  819.  
  820. Version 2.07    (7 December 1996)
  821.  
  822.  
  823. =head1 SEE ALSO
  824.  
  825. perl(1)
  826.  
  827. =cut
  828.